// pages/device/sealTypeSelect/sealTypeSelect.js
import {
  _globle
} from '../../../utils/globle.js'
import lwx from '../../../utils/lwx.js'
import devices from '../../../utils/devices.js'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    items: [{
        name: '蘸墨印章',
        value: '0'
      },
      {
        name: '非蘸墨印章',
        value: '1'
      },
    ],
    isSlect: "",
    pic: ['http://hui-seal.oss-cn-shanghai.aliyuncs.com/upload/xiaochengxu/1912/25/1577252677680_398x289.jpg', 'http://hui-seal.oss-cn-shanghai.aliyuncs.com/upload/xiaochengxu/1912/25/1577252672272_314x256.jpg', 'http://hui-seal.oss-cn-shanghai.aliyuncs.com/upload/xiaochengxu/1912/25/1577252663780_418x410.jpg', 'http://hui-seal.oss-cn-shanghai.aliyuncs.com/upload/xiaochengxu/1912/25/1577252668880_800x600.jpg', 'http://hui-seal.oss-cn-shanghai.aliyuncs.com/upload/xiaochengxu/1912/25/1577252674923_317x220.jpg'],
    devicesCode: '',
    sign: '',
    sealStatus: 'wite'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      devicesCode: options.devicesCode,
      sign: options.sign
    })
  },
  loadDevice: function(status) {
    if (!this._devices) {
      this._devices = new devices(this.data.devicesCode)
    }
    wx.showLoading({
      title: '读取设备信息',
      mask: true
    })
    this._devices.getDevicesInfo().then(res => {
      console.log(res)
      wx.hideLoading()
      wx.showLoading({
        title: '读取状态信息',
        mask: true
      })
      return this._devices.getOperationStatus()
    }).then(res => {
      console.log(res)
      wx.hideLoading()
      this.rand2 = res.rand2;
      this.rand1 = res.rand1;
      return this._devices.setOperationCommand({
        type: status,
        sign: this.data.sign,
        rand1: res.rand1,
        rand2: res.rand2
      })
    }).then(res => {
      return new Promise((resolve, reject) => {
        wx.hideLoading()
        wx.showLoading({
          title: '获取结果...',
          mask: true
        })
        // 循环读取状态
        let statusInterval = setInterval(() => {
          this._devices.getOperationStatus().then(res => {
            // 空闲则是安装完成
            // if (res.operationStatus == 15) {
            //   wx.showLoading({
            //     title: '正在学习蘸墨...',
            //     mask: true
            //   })
            // } 
            if (res.operationStatus !== 1) {
              clearInterval(statusInterval)
              resolve(res.operationStatus)
              //如果是安装完成
              if (status == 4 && this.data.isSlect == '1') {
                wx.showToast({
                  title: '安装已完成',
                  icon: 'none',
                  duration: 1000
                })
                this.installSuccues();
              } else if (status == 4 && this.data.isSlect == '0') {
                this.setData({
                  sealStatus: 'study'
                })
              }
              if (status == 5) {
                this.setData({
                  sealStatus: 'finish'
                })
              }
              if (status == 13) {
                this.installSuccues();
              }
              if (status == 14 || status == 15) {
                wx.showToast({
                  title: '类型选择成功',
                })
              }
            }
          })
        }, 100)
      })
    }).then(res => {
      wx.hideLoading()
    }).catch(err => {
      wx.hideLoading()
      wx.showToast({
        title: '读取数据失败',
        mask: true,
        icon: 'none'
      })
    })
  },
  installSuccues: function() {
    lwx.request({
      url: "install.updateSt",
      data: {
        installStatus: 'PASSED',
        devicesCode: this.data.devicesCode
      }
    }).then(res => {
      console.log('res' + res)
      if (res.data.code == '0') {
        setTimeout(function() {
          wx.switchTab({
            url: '../../index/index',
          })
        }, 500)
      }
    }).catch(err => {
      console.log('err' + err);
    })
  },
  releaseError: function() {
    if (!this._devices) {
      this._devices = new devices(this.data.devicesCode)
    }
    wx.hideLoading()
    wx.showLoading({
      title: '读取状态...',
      mask: true
    })
    this._devices.getOperationStatus().then(res => {
      console.log('进来解锁')
      if (res.operationStatus === 2) {
        wx.hideLoading()
        wx.showLoading({
          title: '解除锁定',
          mask: true
        })
        this._devices.setOperationCommand({
          type: 3,
          sign: this.data.sign,
          rand1: res.rand1,
          rand2: res.rand2
        }).then(res => {
          return new Promise((resolve, reject) => {
            wx.hideLoading()
            wx.showLoading({
              title: '获取结果...',
              mask: true
            })
            // 循环读取状态
            let statusInterval = setInterval(() => {
              this._devices.getOperationStatus().then(res => {
                // 空闲则是安装完成
                if (res.operationStatus !== 1) {
                  clearInterval(statusInterval)
                  resolve(res.operationStatus)
                }
              })
            }, 100)
          })
        }).then(res => {
          wx.hideLoading()
          if (res === 2) {
            wx.showToast({
              title: '解除错误锁定失败',
              mask: true,
              icon: 'none'
            })
          } else {
            wx.showToast({
              title: '解除成功',
              mask: true
            })
          }
        }).catch(err => {
          console.log(err)
          wx.hideLoading()
          wx.showToast({
            title: '解除操作失败',
            mask: true,
            icon: 'none'
          })
        })
      } else {
        wx.hideLoading()
        wx.showToast({
          title: '设备正常，无错误信息',
          mask: true,
          icon: 'none'
        })
      }
    }).catch(res => {
      wx.hideLoading()
      wx.showToast({
        title: '获取运行状态失败',
        mask: true,
        icon: 'none'
      })
    })
  },
  //下一步
  next: function() {
    if (this.data.isSlect == '0') {
      console.log('蘸墨')
      this.loadDevice(14);
      this.setData({
        sealStatus: 'standby'
      })
    } else if (this.data.isSlect == '1') {
      console.log('非蘸墨')
      this.loadDevice(15);
      this.setData({
        sealStatus: 'standby'
      })
    } else {
      wx.showToast({
        title: '请选择印章类型',
        icon: 'none',
        image: '',
      })
    }
  },
  //开始安装
  standby: function() {
    this.loadDevice(5);
  },
  study: function() {
    this.loadDevice(13);
  },
  //安装完成
  finish: function() {
    this.loadDevice(4);
  },

  checkFingerprint: function() {
    var that = this;
    lwx.request({
      url: "legalDefinger.cheak",
      data: {
        devicesCode: that.data.devicesCode
      }
    }).then(res => {
      console.log(res)
      if (res.data.endings.Ending == 'O') {
        // } else if (res.data.endings == 'Y') {
        // } else {
        wx.showToast({
          title: '印章安装完成',
          icon: 'none',
        })
        setTimeout(function() {
          wx.switchTab({
            url: '../../index/index',
          })
        }, 500)
      }
    }).catch(err => {
      console.log('err' + err);
    })
  },
  checkboxChange: function(e) {
    this.setData({
      isSlect: e.detail.value,
    })
  },

  zoomPic: function(e) {
    console.log(e)
    wx.previewImage({
      current: e.currentTarget.dataset.pic, // 当前显示图片的http链接
      urls: this.data.pic // 需要预览的图片http链接列表
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  onUnload: function() {
    if (this._devices) {
      console.log('断开连接')
      this._devices.closeConnection()
    }
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})